<template>
	<view class="app-recommended-product-list dir-left-nowrap" :style="[calcWidth(750)]">
		<scroll-view scroll-y class="app-classification box-grow-0" :style="{height: `${height - 110}rpx`,backgroundColor: catBgColor}">
			<text class="app-class-item"
			      v-for="(item, index) in catList" :key="index"
			      :style="{'color': activeIndex === index ? theme.color : '#353535', 'border-color': activeIndex === index ? theme.border : '' }"
			      @click="goClass(index)"
            v-bind:style="activeIndex === index ? `border-color: ${tagColor};color: ${catSelectedColor}`: `color: ${catUnselectedColor}`"
			>{{item.menuName}}</text>
		</scroll-view>
		<view class="app-product">
			<scroll-view class="app-product-scroll" :scroll-into-view="`scroll-${activeIndex}`" :style="{height: `${height - 110}rpx`}" scroll-y>
				<block v-for="(page, number) in catList" :key="number">
					<view class="app-product-class-item" :style="[calcWidth(750-160-24)]">
						<text class="app-class-name" :style="[calcWidth(750-160-24)]" :id="`scroll-${number}`">{{page.menuName}}</text>
						<view class="app-product-item"
						      v-for="(item, index) in page.goodsList"
						      :key="index"
                              :style="[styleBox, calcWidth(750-160-24-12)]"
						      :class="{'app-product-border': index !== getLength(page.goodsList), 'app-margin-top': index !== 0,'have-bg': goodsStyle < 3}"
						>
							<view class="app-product-border-all"
                                  :style="[calcWidth(750-160-24-12)]"
								:class="{'app-product-border-all-item': goodsStyle === 2}"
							>
								<view @click="jump_router(item)" >
									<view class="out-dialog" v-if="item.goods_stock == 0  && appSetting.is_show_stock == '1'"
                                          :style="{borderRadius: `${cBorderTop}rpx 0 0 ${cBorderBottom}rpx`}">
				                        <image  :src="appSetting.is_use_stock == '1' ? appImg.plugins_out : appSetting.sell_out_pic"></image>
				                    </view>
									<view class="dir-left-nowrap main-between cross-top">
										<image lazy-load="true" class="app-tag-icon" :style="{borderRadius: `${cBorderTop}rpx 0 0 0`}" :src="goodsTagPicUrl" v-if="showGoodsTag"></image>
											<view>
												<app-image :borderRadius="`${cBorderTop}rpx 0 0 ${cBorderBottom}rpx`"
                                                           :img-src="item.cover_pic" width="200rpx" height="200rpx"
												           :mode="fill === 1 ? 'aspectFill' : 'aspectFit'"></app-image>
											</view>
											<view class="app-product-content dir-top-nowrap main-between"
                                                  :style="[calcWidth(750-200-24-152-20)]"
											      :class="{'dir-top-nowrap': !showGoodsName, 'main-right': !showGoodsName}"
											>
												<text class="app-product-name" :style="[calcWidth(750-200-24-152-20-24)]" v-if="showGoodsName">{{item.name}}</text>
												<view class="app-price-buttons  main-between cross-bottom" :class="{'dir-right-nowrap': !showGoodsPrice , 'dir-left-nowrap': showGoodsPrice}">
													<view class="price dir-bottom-nowrap box-grow-1">
														<view style="display: inline-flex;">
								                            <view v-if="item.is_level === 1 && (item.level_price > 0 || item.level_price == 0) && item.is_negotiable != 1" class="app-member-price" :style="{'background':theme.background_p,'color':theme.color}">
								                                <view class="t-omit">会员价
								                                    <text class="price-float">￥</text>
								                                    <text class="price-int">{{item.memberList.priceInt}}</text>
								                                    <text v-if="item.memberList.priceFloat != '.00'" class="price-float">{{item.memberList.priceFloat}}</text>
								                                </view>
								                            </view>
								                            <view v-else-if="isShowOriginalPrice(item)" class="app-original-price">￥{{item.original_price}}</view>
								                        </view>
							                            <view class="app-min-price" :style="{'color': theme.color}">
							                                <text class="price-float" v-if="item.priceList.priceInt > 0 || item.priceList.priceInt == 0">￥</text>
							                                <text class="price-int" :class="item.priceList.priceInt > 0 || item.priceList.priceInt == 0 ? '' :'text'">{{item.priceList.priceInt}}</text>
							                                <text class="price-float">{{item.priceList.priceFloat}}</text>
							                            </view>
														<app-sup-vip v-if="item.vip_card_appoint && item.vip_card_appoint.discount" :discount="item.vip_card_appoint.discount" :is_vip_card_user="item.vip_card_appoint.is_vip_card_user"></app-sup-vip>
													</view>
													<template v-if="showBuyBtn && item.price_content !== appSetting.negotiable_text">
														<button class="app-product-button box-grow-0"
                                                                :style="item.buy_goods_auth ? btnStyle : disableBtnStyle"
														        @click.stop="buyProduct(item)"
														        v-if="buyBtn === 'text'"
														        :class="{
															'app-button-filling': buyBtnStyle === 1,
															'app-button-line' : buyBtnStyle === 2 ,
															'app-button-fillet' : buyBtnStyle === 3,
															'app-button-rounded-lines': buyBtnStyle === 4,
														}"
														>{{buyBtnText}}</button>
														<icon @click.stop="buyProduct(item)"
														      class="app-button-icon app-product-button-cart"
															 :style="{'background-color': item.buy_goods_auth ? theme.background : '#999999','background-image': 'url('+goodsImg.cart+')'}"
														      v-if="buyBtn === 'cart' && item.goods_stock != 0"
														 type></icon>
														<icon @click.stop="buyProduct(item)"
														      class="app-button-icon app-product-button-add"
															 :style="{'background-color': item.buy_goods_auth ? theme.background : '#999999'}"
														      v-if="buyBtn === 'add' && item.goods_stock != 0"
														 type></icon>
													</template>
												</view>
											</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</block>
			</scroll-view>
		</view>
		<view>
			<app-attr
				:goods="goods"
				:attrGroupList="goods.attr_groups"
				:show="show"
				:selectAttr="selectAttr"
				@attr="attr"
				:theme="theme"
			></app-attr>
		</view>
	</view>
</template>

<script>
    import { mapState, mapGetters } from "vuex";
	import appAttr from '../../../components/page-component/app-attr/app-attr.vue';

    export default {
        name: 'app-recommended-product-list',
	    data() {
            return {
                scrollIndex: 0,
	            height: 0,
                activeIndex: 0,
	            cartIndex: -1,
                show: 0,
                selectAttr: {},
	            goods: {},
				disableColor: '#999999',
				disable: 'disable',
                // 规格商品
                attrGoods: {
                    goods: {},
                    attrShow: 0
                },
            }
	    },
	    props: {
            bg: [String],
            cPaddingLr: [String,Number],
            cBorderBottom: [String, Number],
            cBorderTop: [String, Number],
            catList: {
                type: Array,
	            default: function() {
	                return []
	            }
            },
            fill: {
                type: Number,
	            default: function() {
	                return 1;
	            }
            },
            buyBtnText: {
                type: String,
                default: function() {
                    return '';
                }
            },
            buyBtnStyle: {
                type: Number,
                default: function() {
                    return 4;
                }
            },
            showBuyBtn: {
                type: Boolean,
	            default: function() {
	                return true;
	            }
            },
            showGoodsPrice: {
                type: Boolean,
                default: function() {
                    return true;
                }
            },
            buyBtn: {
                type: String,
                default: function() {
                    return 'cart';
                }
            },
            goodsStyle: {
                type: Number,
                default: function() {
                    return 1;
                }
            },
            goodsTagPicUrl: {
                type: String,
                default: function() {
                    return '';
                }
            },
            showGoodsTag: {
                type: Boolean,
                default: function() {
                    return true;
                }
            },
            showGoodsName: {
                type: Boolean,
                default: function() {
                    return true;
                }
            },
            buttonColor: {
                type: String,
                default() {
                    return '';
                }
            },
			theme: Object,
            isUnderLinePrice: {
                type: Boolean,
                default: function() {
                    return false;
                }
            },
			isBuy: {
			    type: Boolean,
			    default: true
			},
            // 是否显示规格
            isShowAttr: {
                type: Boolean,
                default: false
            },
			//标签颜色
			tagColor: {
				type: String
			},
			//选中颜色
			catSelectedColor: {
				type: String
			},
			//未选中颜色
			catUnselectedColor: {
				type: String
			},
			//背景色
			catBgColor: {
				type: String
			}
	    },
        computed: {
            calcWidth() {
                return (width) => {
                    let padding = this.cPaddingLr === undefined ? 0 : Number(this.cPaddingLr) * 2;
                    return {
                        width: `${width - padding}rpx`
                    }
                }
            },
            styleBox() {
                let {
                    bg,
                    cBorderTop,
                    cBorderBottom,
                    goodsStyle
                } = this;
                if (cBorderTop === undefined) {
                    return {};
                }
                return {
                    borderTopLeftRadius: `${cBorderTop}rpx`,
                    borderTopRightRadius: `${cBorderTop}rpx`,
                    borderBottomLeftRadius: `${cBorderBottom}rpx`,
                    borderBottomRightRadius: `${cBorderBottom}rpx`,
                    background: goodsStyle === 3 ? bg : '#FFFFFF',
                }
            },
            calcPicRadius() {
                let {cBorderTop} = this;
                return cBorderTop === undefined ? '16rpx' :`${cBorderTop}rpx`;
            },
            ...mapState({
                appImg: state => state.mallConfig.__wxapp_img.mall,
                appSetting: state => state.mallConfig.mall.setting,
                goodsImg: state => state.mallConfig.__wxapp_img.goods,
                platform: function(state) {
                    return state.gConfig.systemInfo.platform;
                }
            }),
			...mapGetters('mallConfig',{
				getVideo: 'getVideo'
			}),
            btnStyle() {
                let btnStyle = ``;
                if (this.buyBtnStyle === 1 || this.buyBtnStyle === 3) {
                    btnStyle += `background-color: ${this.buttonColor};color: #ffffff;`;
                } else {
                    btnStyle += `border-color: ${this.buttonColor};color: ${this.buttonColor};`;
                }
                return btnStyle;
            },
            disableBtnStyle() {
                let btnStyle = ``;
                if (this.buyBtnStyle === 1 || this.buyBtnStyle === 3) {
                    btnStyle += `background-color: ${this.disableColor};color: #ffffff;`;
                } else {
                    btnStyle += `border-color: ${this.disableColor};color: ${this.disableColor};`;
                }
                return btnStyle;
            },
        },
	    created() {
            let _this = this;
            wx.getSystemInfo({
                success: function(res) {
                    _this.height = (750/res.windowWidth) * res.windowHeight;
                }
            })
	    },
	    methods: {
            getLength(data) {
                return data.length -1;
            },
            // 购买按钮点击事件
            buyProduct(goods) {
                if(this.isBuy) {
                    if (this.isShowAttr) {
                        this.attrGoods.goods = goods;
                        this.attrGoods.attrShow = Math.random();
                    } else {
                        uni.showLoading({
                            text: '',
                            mask: true
                        });
                        this.$request({
                            url: this.$api.goods.attr,
                            data: {
                                id: goods.id,
                                mch_id: goods.mch_id
                            }
                        }).then(e => {
                            uni.hideLoading();
                            if (e.code === 0) {
                                let data = Object.assign(goods, e.data);
                                this.$emit('buyProduct', {
                                    goods: data,
                                    attrShow: Math.random()
                                });
                            } else {
                                uni.showToast({
                                    title: e.msg,
                                    icon: 'none'
                                })
                            }
                        })
                    }
                }else {
                    this.jump_router(goods);
                }
            },
            goClass(index) {
                this.activeIndex = index;
            },
            attr(data) {},
			jump_router(data) {
				// #ifndef MP-BAIDU
				if (data.video_url && this.getVideo == 1) {
				    // #ifdef MP
					uni.navigateTo({
						url: `/pages/goods/video?goods_id=${data.id}&sign=${data.sign}`
					});
					// #endif
                    // #ifdef H5
                    uni.navigateTo({
                        url: data.page_url
                    });
                    // #endif
				} else {
					uni.navigateTo({
						url: data.page_url
					});
				}
				// #endif
				// #ifdef MP-BAIDU
				uni.navigateTo({
					url: data.page_url
				});
				// #endif
			},
            // 是否显示原价
            isShowOriginalPrice(goods) {
                return this.isUnderLinePrice && goods.is_negotiable !== 1 ? 1 : 0;
            },
	    },
	    components: {
            'app-attr': appAttr
	    }
    }
</script>

<style lang="scss">
	.app-class-item {
		display: inline-block;
		width: #{158rpx};
		line-height: #{104rpx};
		font-size: #{26rpx};
		text-align: center;
        overflow: hidden;
        text-overflow:ellipsis;
        white-space: nowrap;
		border-left: #{2rpx} solid transparent;
	}
	.app-recommended-product-list {
		width: #{750rpx};
		height: #{100%};

        ::-webkit-scrollbar {
            width: 0;
            height: 0;
            color: transparent;
        }
		.app-classification {
			width: #{160rpx};
			height: #{100%};
		}
		.app-product {
			height: #{100%};
			padding-left: #{24rpx};
			padding-top: #{4rpx};
			.app-product-class-item {
				width: #{750-200-24rpx};
				.app-class-name {
					display: inline-block;
					width: #{750-200-24rpx};
					padding-top: #{40rpx};
					padding-bottom: #{36rpx};
					font-size: #{24rpx};
					color: #666666;
				}
				.app-product-item {
					width: #{750-200-24-12rpx};
					border-radius: #{16rpx};
					&.have-bg {
						background-color: #fff;
					}
					position: relative;
					.app-tag-icon {
						position: absolute;
						top: 0;
						left: 0;
						width: #{55rpx};
						height: #{55rpx};
						z-index: 1000;
					}
				    .out-dialog {
				        width: #{200rpx};
				        height: #{200rpx};
				        position: absolute;
				        z-index: 10;
				        background-color: rgba(0,0,0,.5);
				        image {
				            width: #{200rpx};
				            height: #{200rpx};
				            border-radius: #{16rpx};
				        }
				    }
					.app-product-image {
						/* #ifndef MP-ALIPAY */
						width: #{150rpx};
						/* #endif */
						/* #ifdef MP-ALIPAY */
						width: #{136rpx};
						/* #endif */
						/* #ifndef MP-ALIPAY */
						height: #{150rpx};
						/* #endif */
						/* #ifdef MP-ALIPAY */
						height: #{136rpx};
						/* #endif */
					}
					.app-product-content {
						width: #{750-200-24-152-20rpx};
						min-height: #{200upx};
						padding: 0 #{21rpx} #{23rpx};
						.app-product-name {
							font-size: #{28rpx};
							color: #353535;
							padding-top: #{15rpx};
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 1;
							overflow: hidden;
							width: #{750-200-24-152-20-24rpx};
							margin-bottom: #{10rpx};
						}
						.app-price-buttons {
							
							//width: #{750-200-24-152-20-24-24rpx};
							.app-price {
								font-size: #{28rpx};
							}
							.app-product-button {
								display: inline-block;
								padding: 0 #{20rpx};
								height: #{48rpx};
								font-size: #{28rpx};
								color: #ffffff;
								line-height: #{48rpx};
								border-radius: 0;
								margin: 0;
							}
							.app-button-filling {
							}
							.app-button-fillet {
								border-radius: #{999rpx} !important;
							}
							.app-button-line {
							}
							.app-button-rounded-lines {
								border-radius: #{20rpx} !important;
							}
							.app-product-button-add {
								background-image: url("../../../static/image/icon/add-to.png");
							}
						}
						.price {
					        .app-min-price {
					            font-family: Alibaba;
					            font-size: #{32rpx};
					            margin-top: 10rpx;
					            line-height: 1;
					            margin-right: 14rpx;
                                .text {
                                    font-size: 30rpx;
                                }
					            .price-float {
					                font-size: 24rpx;
					            }
					        }
					        .app-member-price {
					            font-size: 24rpx;
					            padding: 0 6rpx;
					            overflow: hidden;
					            white-space: nowrap;
					            display: inline-block;
					        }
					        .app-original-price {
					            font-size: #{24rpx};
					            color: #999999;
					            text-decoration: line-through;
					            line-height: 1;
					        }
						}
					}
					.app-product-border-all {
						width: #{750-200-24-12rpx};
						border-radius: #{10rpx};
					}
					.app-product-border-all-item {
						border: #{1rpx} solid #e2e2e2;
					}
					.app-button-icon {
						background-repeat: no-repeat;
						background-size: 100% 100%;
						width: #{50rpx};
						height: #{50rpx};
					}
				}
				.app-product-border {
					border-bottom-color: #e2e2e2;
				}
				.app-margin-top {
					margin-top: #{24rpx};
				}
			}
		}
	}
	.app-volume {
		margin: #{8rpx 0};
		font-size: #{22rpx};
		color: #666666;
	}

    .bd-original-price {
        color: #999999;
        font-size: 22upx;
        text-decoration: line-through;
        margin-left: 10upx;
    }
</style>
